﻿using System;
using System.Data;
using System.Data.OracleClient;
using Maticsoft.DBUtility;

namespace Xiaov.SmartWeb.Application.Models
{
  /// <summary>
  /// XMLVERSION:实体类(属性说明自动提取数据库字段的描述信息)
  /// </summary>
  [Serializable]
  public partial class XMLVERSION
  {
    public XMLVERSION()
    { }
    #region Model
    private decimal _year;
    private string _bbbm;
    private string _caption;
    private string _coordinates;
    private string _hiddencolumn;
    private string _hiddenrow;
    private string _notalloweditrow;
    private string _lockcell;
    private string _columnwidth;
    private string _namehierarchy;
    private string _selectfield;
    private string _orderby;
    private int _rowcount;
    private int _columncount;
    private string _NotAllowEditColumn;
    private string _Title;

    public string Title
    {
      get { return _Title; }
      set { _Title = value; }
    }

    public string NotAllowEditColumn
    {
      get { return _NotAllowEditColumn; }
      set { _NotAllowEditColumn = value; }
    }

    public int COLUMNCOUNT
    {
      get { return _columncount; }
      set { _columncount = value; }
    }

    public int ROWCOUNT
    {
      get { return _rowcount; }
      set { _rowcount = value; }
    }
    /// <summary>
    /// 
    /// </summary>
    public decimal YEAR
    {
      set { _year = value; }
      get { return _year; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string BBBM
    {
      set { _bbbm = value; }
      get { return _bbbm; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string CAPTION
    {
      set { _caption = value; }
      get { return _caption; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string COORDINATES
    {
      set { _coordinates = value; }
      get { return _coordinates; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string HIDDENCOLUMN
    {
      set { _hiddencolumn = value; }
      get { return _hiddencolumn; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string HIDDENROW
    {
      set { _hiddenrow = value; }
      get { return _hiddenrow; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string NOTALLOWEDITROW
    {
      set { _notalloweditrow = value; }
      get { return _notalloweditrow; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string LOCKCELL
    {
      set { _lockcell = value; }
      get { return _lockcell; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string COLUMNWIDTH
    {
      set { _columnwidth = value; }
      get { return _columnwidth; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string NAMEHIERARCHY
    {
      set { _namehierarchy = value; }
      get { return _namehierarchy; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string SELECTFIELD
    {
      set { _selectfield = value; }
      get { return _selectfield; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string ORDERBY
    {
      set { _orderby = value; }
      get { return _orderby; }
    }
    #endregion Model

    public XMLVERSION(string nf, string yf, string dwbm, string tableName)
    {
      InitVersion(nf, yf, tableName, dwbm);
    }
    private void InitVersion(string nf, string yf, string tableName, string dwbm)
    {
      string sql = "select * from xmlversion_dw_nf_yf where nf=:0 and bbbm=:1 and yf=:2 and dwbm=:3";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20),
                                 new OracleParameter(":2", OracleType.VarChar, 20), new OracleParameter(":3", OracleType.VarChar, 20)};
      parms[0].Value = nf;
      parms[1].Value = tableName;
      parms[2].Value = yf;
      parms[3].Value = dwbm;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 0)//当本年没有时
      {
        LoadProc("p_createversion", tableName, dwbm, nf, yf);
        sql = "select * from xmlversion_dw_nf_yf where nf=:0 and bbbm=:1 and yf=:2 and dwbm=:3";
        parms = new OracleParameter[4] { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20),
                                 new OracleParameter(":2", OracleType.VarChar, 20), new OracleParameter(":3", OracleType.VarChar, 20)};
        parms[0].Value = nf;
        parms[1].Value = tableName;
        parms[2].Value = yf;
        parms[3].Value = dwbm;
        dt = DbHelperOra.Query(sql, parms).Tables[0];
      }
      if (dt.Rows.Count == 1)
      {
        SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        CAPTION = dt.Rows[0]["caption"].ToString();
        COORDINATES = dt.Rows[0]["coordinates"].ToString();
        COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        Title = dt.Rows[0]["title"].ToString();
        HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
        NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();
        LOCKCELL = dt.Rows[0]["lockcell"].ToString();
        HIDDENROW = dt.Rows[0]["hiddenrow"].ToString();
        ORDERBY = dt.Rows[0]["orderby"].ToString();
        InitVersionDW_Col(nf, tableName, dwbm);
        InitVersionDW_Row(nf, tableName, dwbm);
        InitVersionDW_lockcell(nf, tableName, dwbm);
      }
    }

    public bool LoadProc(string ProcdureName, string tableName, string dwbm, string nf, string yf)
    {
      OracleParameter[] parameters ={
            new OracleParameter("v_bbbm",OracleType.VarChar,20),
　　     new OracleParameter("v_dwbm",OracleType.VarChar,20),
　　     new OracleParameter("v_nf",OracleType.Number,4),
            new OracleParameter("v_yf",OracleType.Number,2)
　　      };
      parameters[0].Value = tableName.ToUpper();
      parameters[1].Value = dwbm.ToUpper();
      parameters[2].Value = nf;
      parameters[3].Value = yf;
      try
      {
        OracleDataReader odr = Maticsoft.DBUtility.DbHelperOra.RunProcedure(ProcdureName, parameters); odr.Close();
        return true;
      }
      catch
      {
        return false;
      }
    }
    private void InitVersion(string nf, string tableName, string dwbm)
    {
      string sql = "select * from xmlversion where year=:0 and bbbm=:1";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) };
      parms[0].Value = nf;
      parms[1].Value = tableName;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count != 1)//当本年没有时
      {
        sql = "select max(year) from xmlversion";
        nf = DbHelperOra.GetSingle(sql).ToString();
        sql = "select * from xmlversion where year=:0 and bbbm=:1";
        parms = new OracleParameter[] { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) };
        parms[0].Value = nf;
        parms[1].Value = tableName;
        dt = DbHelperOra.Query(sql, parms).Tables[0];
      }
      if (dt.Rows.Count == 1)
      {
        SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        CAPTION = dt.Rows[0]["caption"].ToString();
        COORDINATES = dt.Rows[0]["coordinates"].ToString();
        COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        Title = dt.Rows[0]["title"].ToString();
        HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
        NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();
        LOCKCELL = dt.Rows[0]["lockcell"].ToString();
        HIDDENROW = dt.Rows[0]["hiddenrow"].ToString();
        ORDERBY = dt.Rows[0]["orderby"].ToString();
        InitVersionDW_Col(nf, tableName, dwbm);
        InitVersionDW_Row(nf, tableName, dwbm);
        InitVersionDW_lockcell(nf, tableName, dwbm);
      }
    }
    private void InitVersionDW_Col(string nf, string tableName, string dwbm)
    {
      string sql = "select * from xmlversion_dw_hidecol where year=:0 and bbbm=:1 and dwbm=:2";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) 
                                , new OracleParameter(":2", OracleType.VarChar, 20) };
      parms[0].Value = nf;
      parms[1].Value = tableName;
      parms[2].Value = dwbm;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 1)
      {
        //SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        //CAPTION = dt.Rows[0]["caption"].ToString();
        //COORDINATES = dt.Rows[0]["coordinates"].ToString();
        //COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        //ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        //Title = dt.Rows[0]["title"].ToString();
        HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
        //HIDDENROW = dt.Rows[0]["hiddenrow"].ToString();
        //NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();
      }
    }
    private void InitVersionDW_Row(string nf, string tableName, string dwbm)
    {
      string sql = "select * from xmlversion_dw_hiderow where year=:0 and bbbm=:1 and dwbm=:2";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) 
                                , new OracleParameter(":2", OracleType.VarChar, 20) };
      parms[0].Value = nf;
      parms[1].Value = tableName;
      parms[2].Value = dwbm;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 1)
      {
        //SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        //CAPTION = dt.Rows[0]["caption"].ToString();
        //COORDINATES = dt.Rows[0]["coordinates"].ToString();
        //COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        //ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        //Title = dt.Rows[0]["title"].ToString();
        //HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
        HIDDENROW = dt.Rows[0]["hiddenrow"].ToString();
        //NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();
      }
    }
    private void InitVersionDW_lockcell(string nf, string tableName, string dwbm)
    {
      string sql = "select * from XMLVERSION_DW_LOCKCELL where year=:0 and bbbm=:1 and dwbm=:2";
      OracleParameter[] parms = { new OracleParameter(":0", OracleType.VarChar, 10), new OracleParameter(":1", OracleType.VarChar, 20) 
                                , new OracleParameter(":2", OracleType.VarChar, 20) };
      parms[0].Value = nf;
      parms[1].Value = tableName;
      parms[2].Value = dwbm;
      DataTable dt = DbHelperOra.Query(sql, parms).Tables[0];
      if (dt.Rows.Count == 1)
      {
        LOCKCELL = dt.Rows[0]["lockcell"].ToString();
        //SELECTFIELD = dt.Rows[0]["selectfield"].ToString();
        //CAPTION = dt.Rows[0]["caption"].ToString();
        //COORDINATES = dt.Rows[0]["coordinates"].ToString();
        //COLUMNCOUNT = int.Parse(dt.Rows[0]["columncount"].ToString());
        //ROWCOUNT = int.Parse(dt.Rows[0]["rowcount"].ToString());
        //Title = dt.Rows[0]["title"].ToString();
        //HIDDENCOLUMN = dt.Rows[0]["hiddencolumn"].ToString();
        //HIDDENROW = dt.Rows[0]["hiddenrow"].ToString();
        //NotAllowEditColumn = dt.Rows[0]["notalloweditcolumn"].ToString();
      }
    }
  }
}

